var router=require('express').Router();
var Note=require('../models/note');

module.exports=router;

router.get('/',(req,res)=>{
    var now=Date.now();
    var notes=[],cnt=105;

    for(var i=0;i<cnt;i++){
        var time=now-(1000*3600*24*(Math.random()*10-5)|0);
        var expiration=time+(1000*3600*24*Math.random()*3|0);
        notes.push({
            //note:'Note-'+i,
            createAt:time,
            createAtTime:time,
            expiration:expiration,
            expirationTime:+new Date(expiration),
            desc:'Desc-'+time,
            status:expiration<now?-1:Math.random()<0.5?0:1
        });
    }
    notes.sort((a,b)=>{
        return a.createAtTime-b.createAtTime;
    });
    notes.forEach((note,i)=>{
        note.note='Note-'+i;
    });

    Note.remove({},(err)=>{
        if(err)return console.log(err);

        res.json(notes);

        1&&(function saveIIFE(notes){
            var note=notes.shift();
            if(!note){
                console.info('Over!');
                return;
            }

            new Note(note).save((err,ret)=>{
                console.log(err,ret);
                saveIIFE(notes);
            });
        })(notes.slice().sort((a,b)=>{
            return a.createAt-b.createAt;
        }));
    });


});